package com.youlu.campus.admin.auth.controller;

import com.youlu.campus.admin.auth.entity.exception.AuthErrorCode;
import com.youlu.campus.admin.auth.service.ResourceService;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.auth.service.UserService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by zhuhuaiqi
 *
 */
@RestController
public class LoginController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    ResourceService resourceService;


    @RequestMapping("/logout")
    public RestResult logout() {

        //UserStatus #handleOffline 、#handleOnline、 #handleSuspend
        String userId = AuthUtils.getUserId();
        String comId = AuthUtils.getComId();

        logger.info("LoginController.logout userId:{}, comId:{}", userId, comId);


        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        // 清除缓存
        userService.logoutCleanCache(comId, userId);
        logger.debug("logout ====>>>>>>>>>>>>>>>>");
        return RestResultBuilder.builder().errorCode(AuthErrorCode.LOGOUT_SUCCESS).build();
    }
}
